import Common from "../common/Common";
import ViewBase from "./ViewBase";
import LevelProgress from "../LevelProgress";
import UserInfo from "../UserInfo";
import GameManager from "../manager/GameManager";
import FrameAnimation from "../common/FrameAnimation";


// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class BattleViewCtr extends ViewBase {


    @property(cc.Label)
    textGold: cc.Label = null;

    @property(cc.Label)
    textCurScore: cc.Label = null;
    
    @property(cc.Node)
    nodeGoldTextParent: cc.Node = null;

    @property(cc.Node)
    nodeLevelProgress: cc.Node = null;

    @property(cc.Node)
    node_addPower: cc.Node = null;

    @property(cc.Node)
    btn_addPower: cc.Node = null;

    @property(cc.Node)
    node_newUserPrompt:cc.Node

    @property(cc.Label)
    text_time: cc.Label = null;

    
    isClickShare:boolean = false
    private levelProgress:LevelProgress;

    private nodeGoldTextParentInitPos:cc.Vec2 = null;

    private isCanRunAction:boolean = true;

    timeS:number = 0;
    timeMs:number = 99;

    isShowingPrompt:boolean = false
    
 

    totalTimes:number = 0;
   
    isTimeDownCount:boolean = false;
    isTimeOut:boolean = false;

    initView(){
        this.nodeGoldTextParentInitPos = this.nodeGoldTextParent.position;
        this.levelProgress = this.nodeLevelProgress.getComponent<LevelProgress>(LevelProgress);
    }

     //刷新UI 
    refreshView(isFristRefresh:boolean = false){
        this.refreshGoldCound();
        this.levelProgress.setLevelProgressInfo(Common.curLevel,Common.maxLevelScore);       
        this.levelProgress.setProgress(Common.curLevelScore);
        this.refreshScore(!isFristRefresh);
        this.refreshGoldCound()
        this.isClickShare =  false

        this.isTimeDownCount = false;
        this.isTimeOut = false;

        this.btn_addPower.active = false
        this.node_addPower.active = false

        this.node_newUserPrompt.setScale(1,0)
        this.isShowingPrompt = false;
    }

    addEvent(){
        Common.addClickEvent(this.btn_addPower,this.onClick.bind(this));
    }
    
    onClick(tag:string){
        if(tag == "btn_addPower"){
            GameManager.instance.wxHelper.shareAppMessage("");
            this.isClickShare = true
        }
    }

    update(dt){
        if(this.isTimeOut || !this.isTimeDownCount){
            return
        }
        this.totalTimes = this.totalTimes + dt
        if(this.totalTimes >= 1){
            this.totalTimes  = 0
            this.timeS--
        }
        let strS:string = this.timeS.toString();
        if(this.timeS < 10){
            if(this.timeS < 0){
                this.timeS = 0
            }
            strS = "0"+strS.toString();
        }

        let strMs:string = ""
        if(this.totalTimes >= 0.001){
            this.timeMs = this.timeMs - 1
            if(this.timeMs <= 0){
                if(this.timeS <= 0){
                    this.isTimeOut = true
                    //倒计时效果结束了
                    Common.addPower = 1
                    this.node_addPower.active = false
                    GameManager.instance.curCannonCtr.addPower(false)
                    this.timeMs = 0
                }else{
                    this.timeMs = 99
                }
            }
        }

        if(this.timeMs < 10){
            strMs = "0" + this.timeMs.toString()
        }else{
            strMs = this.timeMs.toString();
        }

        strS = strS + ":" + strMs;
        this.text_time.string = strS
    }

    //刷新金币数量
    refreshGoldCound(){
        this.textGold.string = Common.getShowNumber(UserInfo.curGold);
        let x = 60*this.textGold.string.length*-1-36;
        this.textGold.node.children[0].position = new cc.Vec2(x,-24);
        x = this.nodeGoldTextParentInitPos.x + (this.textGold.string.length - 1)*24;
        this.nodeGoldTextParent.position = new cc.Vec2(x,this.nodeGoldTextParentInitPos.y);
    }
    //刷新分数
    refreshScore(isAction:boolean = true){
        this.textCurScore.string = Common.totalShowScore.toString();
        if(isAction && this.isCanRunAction){
            this.isCanRunAction = false
            this.textCurScore.node.setScale(0.45,0.45);
            this.textCurScore.node.runAction(cc.sequence(cc.scaleTo(0.1,0.62,0.62),cc.callFunc(function(){
                this.isCanRunAction = true
            }.bind(this))));
        }
    }
    
    refreshGoldInfo(){
        this.refreshGoldCound();
    }

    refreshProgressInfo(){
        this.levelProgress.setLevelProgressInfo(Common.curLevel,Common.maxLevelScore);       
        this.levelProgress.setProgress(Common.curLevelScore);
    }

    //额外的事件调用
    executeEvent(eventTag:string = "defualt"){
        if(eventTag == "share"){
            if(this.isClickShare){
                this.isClickShare = false
                this.btn_addPower.active = false
                this.isTimeDownCount = true
                this.node_addPower.active = true
                this.totalTimes = 0
                this.timeS = 14
                Common.addPower = 2
                GameManager.instance.curCannonCtr.addPower(true);

            }
        }else if(eventTag == "showAddPower"){
            this.btn_addPower.active = true
            this.node_addPower.active = false
            this.isTimeDownCount = false;
            this.isTimeOut = false;
            this.btn_addPower.active = true
            this.node_addPower.active = false
            Common.addPower = 1
        }else if(eventTag == "refreshGold"){
            this.refreshGoldInfo()
        }else if(eventTag == "refreshScore"){
            this.refreshScore();
        }else if(eventTag == "refreshProgressInfo"){
            this.refreshProgressInfo();
        }else if(eventTag == "hideAddPower"){
            this.isTimeDownCount = false;
            this.isTimeOut = false;
            Common.addPower = 1
            this.btn_addPower.active = false
            this.node_addPower.active = false
            GameManager.instance.curCannonCtr.addPower(false)
        }else if(eventTag == "showPrompt"){
            if(this.isShowingPrompt){
                return;
            }

            this.node_newUserPrompt.runAction(cc.sequence(cc.scaleTo(0.2,1,1),cc.delayTime(8),cc.callFunc(function(){
                if(this.isShowingPrompt){
                    this.node_newUserPrompt.runAction(cc.sequence(cc.scaleTo(0.2,1,0),cc.callFunc(function(){
                        this.isShowingPrompt = false
                    }.bind(this)),));
                }
            }.bind(this))));
            this.isShowingPrompt = true
        }else if(eventTag == "hidePrompt"){
            this.node_newUserPrompt.setScale(1,0)
            this.isShowingPrompt = false;
        }
    }
}
